package cloud.eastfair;

import com.eastfair.generator.CodeGenerator;
import com.eastfair.generator.config.CodeGeneratorConfig;
import com.eastfair.generator.config.FileCreateConfig;
import com.eastfair.generator.type.EntityFiledType;
import com.eastfair.generator.type.EntityType;
import com.eastfair.generator.type.GenerateType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
 * 昆仑亿发代码自动生成工具
 * 代码生成权限系统的代码
 *
 * @author ligang
 * @date 2019/05/25
 */
public class JavaGeneratorQuestionnaire {
    public static void main(String[] args) {

        //解析配置文件信息
        loalPropertiesData();
        //执行自动代码生成
        executeGenerator();
    }

    public static void loalPropertiesData() {
        try {
            Properties properties = new Properties();
            Resource resource = new ClassPathResource("generator_questionnaire.properties");
            InputStream inputStream = null;
            inputStream = resource.getInputStream();
            properties.load(inputStream);
            GeneratorModel.setMysqlUrl(properties.getProperty("jdbc.connectionURL"));
            GeneratorModel.setUserName(properties.getProperty("jdbc.userName"));
            GeneratorModel.setPassword(properties.getProperty("jdbc.password"));
            GeneratorModel.setTablePrefix(properties.getProperty("mysql.tablePrefix"));
            GeneratorModel.setTables(Arrays.asList(properties.getProperty("mysql.tables").split(",")));
            GeneratorModel.setAuthor(properties.getProperty("com.author"));
            GeneratorModel.setPath(properties.getProperty("com.dirPath"));
            GeneratorModel.setGroupId(properties.getProperty("com.groupId"));
            GeneratorModel.setServiceName(properties.getProperty("com.serviceName"));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void executeGenerator() {
        CodeGeneratorConfig build = buildAuthEntity();
        // 项目、配置文件的前缀
        build.setProjectPrefix("");
        build.setGroupId(GeneratorModel.getGroupId());
        build.setIsBoot(false);
        // 指定全部代码的生成策略
        GenerateType generate = GenerateType.OVERRIDE;
//        generate = null;
        // 构造器传入null，下面设置的策略（setGenerate*）才会生效， 构造器传入不为null时，下面设置的策略（setGenerate*）无效，将全部使用构造器传入的策略
        FileCreateConfig fileCreateConfig = new FileCreateConfig(generate);
        fileCreateConfig.setGenerateEntity(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateEnum(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateDto(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateXml(GenerateType.OVERRIDE);
        fileCreateConfig.setGenerateDao(GenerateType.IGNORE);
        fileCreateConfig.setGenerateServiceImpl(GenerateType.IGNORE);
        fileCreateConfig.setGenerateService(GenerateType.IGNORE);
        fileCreateConfig.setGenerateController(GenerateType.IGNORE);
        build.setFileCreateConfig(fileCreateConfig);

        //手动指定枚举类 生成的路径
        Set<EntityFiledType> filedTypes = new HashSet<>();
        filedTypes.addAll(Arrays.asList(
//                EntityFiledType.builder().name("httpMethod").table("c_common_opt_log")
//                        .packagePath("com.eastfair.common.enums.HttpMethod").gen(GenerateType.IGNORE).build()
        ));
        build.setFiledTypes(filedTypes);

        build.setPackageBase(build.getGroupId() + "." + build.getChildModuleName());
        // 运行
        CodeGenerator.run(build);
    }


    private static CodeGeneratorConfig buildAuthEntity() {
        CodeGeneratorConfig build = CodeGeneratorConfig.
                build(GeneratorModel.serviceName, "", GeneratorModel.author, GeneratorModel.tablePrefix, GeneratorModel.tables);
        build.setSuperEntity(EntityType.ENTITY);
//        build.setChildPackageName("auth");
        build.setUrl(GeneratorModel.mysqlUrl);

        //mysql 账号密码
        build.setUsername(GeneratorModel.userName);
        build.setPassword(GeneratorModel.password);
        build.setProjectRootPath(GeneratorModel.path);

        return build;
    }
}
